<?php

namespace app\data\logic;

use app\data\service\VideosService;

class VideosLogic extends BaseLogic
{
    protected $videosService;

    public function __construct()
    {
        $this->videosService = new VideosService();
    }

    /**
     * 社媒列表
     * @param $params
     * @return array
     */
    public function getVideosList($params)
    {
        $field = '*';
        $where = [];
        $order = 'sort desc,id desc';
        $pageIndex = $params['page'] ?? 1;
        $pageSize = $params['limit'] ?? 10;
        $result = $this->videosService->getPageList($pageIndex, $pageSize, $where, $field, $order);
        if (array_key_exists('all', $params)) {
            $result  = $this->videosService->getAll($field, $where, $order);
        }

        return $result;
    }


    /**
     * 添加社媒
     * @param $data
     * @return array
     */
    public function createNewVideos($data): array
    {
        try {
            $ret = $this->videosService->insert($data);
            if ($ret) {
                return $this->responseInfo(true, 'SUCCESS');
            } else {
                return $this->responseInfo(false, 'FAILURE');
            }
        } catch (\Exception $e) {
            return $this->responseInfo(false, $e->getMessage());
        }
    }

    /**
     * 更新社媒
     * @param $id
     * @param $data
     * @return array
     */
    public function updateVideos($id, $data): array
    {
        try {
            $where = [
                'id' => $id
            ];
            $ret = $this->videosService->update($where, $data);
            if ($ret) {
                return $this->responseInfo(true, 'SUCCESS');
            } else {
                return $this->responseInfo(false, 'FAILURE');
            }
        } catch (\Exception $e) {
            return $this->responseInfo(false, $e->getMessage());
        }
    }

    /**
     * 社媒详情
     * @param $id
     * @return array
     */
    public function getVideosDetail($id): array
    {
        try {
            $where = [
                'id' => $id
            ];
            $res = $this->videosService->getOne('*', $where);
            if ($res) {
                return $this->responseInfo(true, 'SUCCESS', $res);
            } else {
                return $this->responseInfo(false, 'FAILURE');
            }
        } catch (\Exception $e) {
            return $this->responseInfo(false, $e->getMessage());
        }
    }

    /**
     * 删除社媒
     * @param $id
     * @return array
     */
    public function deleteVideos($id): array
    {
        try {
            $where = [
                'id' => $id
            ];
            $ret = $this->videosService->softDel($where);
            if ($ret) {
                return $this->responseInfo(true, 'SUCCESS');
            } else {
                return $this->responseInfo(false, 'FAILURE');
            }
        } catch (\Exception $e) {
            return $this->responseInfo(false, $e->getMessage());
        }
    }

}